package xyz.mumuwei.youthcourseserver.controller;


import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.mumuwei.youthcourseserver.common.dto.PageDto;
import xyz.mumuwei.youthcourseserver.common.lang.Result;
import xyz.mumuwei.youthcourseserver.pojo.Course;
import xyz.mumuwei.youthcourseserver.service.*;

/**
 * @author mumuwei
 * @since 2022-02-19
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/course")
public class CourseController {

    final CourseService courseService;
    final CourseTimeService timeService;
    final CourseRotationService rotationService;
    final FileService fileService;
    final UserInfoService teacherService;
    final CourseExperienceService experienceService;

    /**
     * 课程类别-分页
     */
    @GetMapping("/page")
    public Result page(PageDto pageDto, @RequestAttribute Integer uid){
        return courseService.getCoursePage(pageDto, uid);
    }

    /**
     * 课程列表-不分页
     */
    @GetMapping("/list")
    public Result list(){
        return courseService.getCourseList();
    }

    /**
     * 获取本人今日课程列表
     * @param uid 用户账户 Id
     * @return 课程列表
     */
    @GetMapping("/today")
    public Result getTodayCourseList(@RequestAttribute Integer uid) {
        return courseService.getTodayCourseList(uid);
    }

    /**
     * 获取本人今日课程列表
     * @param uid 用户账户 Id
     * @return 课程列表
     */
    @GetMapping("/student")
    public Result getStudentCourseList(@RequestAttribute Integer uid) {
        return courseService.getStudentCourseList(uid);
    }

    /**
     * 根据课程分类查询课程列表
     * @param pageDto 分页信息
     * @return 返回查询数据
     */
    @GetMapping("/page/type")
    public Result getPageByType(PageDto pageDto) {
        return courseService.getPageByType(pageDto);
    }

    /**
     * 添加课程
     * @param course 课程信息
     */
    @PostMapping("")
    public Result addCourse(@Validated @RequestBody Course course) throws Exception {
        return courseService.addCourse(course);
    }

    /**
     * 更新课程
     * @param course 更新后的课程信息
     */
    @PostMapping("/update")
    public Result update(@Validated @RequestBody Course course) throws Exception {
        return courseService.updateCourse(course);
    }

    /**
     * 根据Id删除课程信息
     */
    @GetMapping("/del_{id}")
    public Result delete(@PathVariable Integer id){
        return courseService.deleteCourseById(id);
    }

    /**
     * 根据id 获取课程信息
     */
    @GetMapping(value = { "/{id}", "cs/{id}" })
    public Result getCourseById(@PathVariable Integer id){
        return courseService.getCourseById(id);
    }

    /**
     * 根据id 更新课程状态
     */
    @PostMapping("/state")
    public Result getCourseById(Integer id, Integer state){
        if (courseService.updateById(new Course().setId(id).setState(state))) {
            return Result.success("更新课程状态成功！");
        }
        return Result.fail("更新失败！");
    }

    /**
     * 获取所有适合年龄段列表
     */
    @GetMapping("/age")
    public Result getAgeList(){
        return courseService.getAgeList();
    }

    /**
     * 获取所有课程分类列表
     */
    @GetMapping("/classify")
    public Result getTypeList(){
        return courseService.getTypeList();
    }

    /**
     * 判断学生是否已购买该课程
     * @param uid 用户Id
     * @param courseId 课程id
     * @return 是否购买
     */
    @GetMapping("/check{courseId}")
    public Result checkStudentBuyCourse(@RequestAttribute Integer uid, @PathVariable Integer courseId) {
        return courseService.checkStudentBuyCourseByUserId(uid, courseId);
    }

    /**
     * 根据用户身份获取课程列表(不分页)
     * @param uid 用户id
     * @return 课程列表
     */
    @GetMapping("/courseList")
    public Result getCourseListByRole(@RequestAttribute Integer uid) {
        return courseService.getCourseListByRole(uid);
    }

    /**
     * 课程销量前五
     */
    @GetMapping("/courseSales")
    public Result getCourseSales() {
        return courseService.getCourseSales();
    }
}

